/**
 * FancyUpload jQuery版
 * 
 *
 * @version		0.1 (original version 3.0)
 *
 * @license		MIT License
 *
 * @author		Harald Kirschner <http://digitarald.de>
 * @author		Makoto Yuki <http://bouyke.com>
 * @copyright	Authors
 */

/**
 * Swiffクラスの代替
 */
var Swiff = {
	// 新しいアップローダを追加する
	register: function (object) {
		var id = object.id;
		this.Uploader[id] = object;
		return;
	},
	
	Uploader : {
		// verboseログを取得する
		log: function (label,string){
			console.info (label,string);
		},
	},

	/**
	 * 絶対パスの取得
	 */
	qualifyPath: function (path) {
		var anchor;
		(anchor || (anchor = $('<a />')[0])).href = path;
		return anchor.href;
	}
};

/**
 * アップローダオブジェクトのスケルトン
 */
var Uploader = function () {
	return {
		// swfのインスタンス
		instance: null,

		options: {
			typeFilter: null,
			typeFilterDescription: null,
			multiple: null,
			queued: null,
			verbose: null,
			width: 128,
			height: 32,
			passStatus: null,
			
			url: null,
			method: null,
			data: null,
			mergeData: null,
			fieldName: null,
			progressGraphSize: 10,
			
			fileSizeMin: 1,
			fileSizeMax: null, // Official limit is 100 MB for FileReference, but I tested up to 2Gb!
			allowDuplicates: false,
			timeLimit: 30,
//			timeLimit: (Browser.Platform.linux) ? 0 : 30,
			
			policyFile: null,
			buttonImage: null,
		},
		params: {},
		properties: {},

		// 位置合わせを行う
		setPosition: function () {
			var target = $(this.target);
			var nObj = $('#' + this.id);
			var position = target.position();

			nObj.css ('position', 'absolute');
			nObj.css ('top', position.top);
			nObj.css ('left', position.left);
			nObj.css ('width', target.width());
			nObj.css ('height', target.height());
			nObj.css ('z-index', this.zIndex);
		},

		// swfからのコールバックを受け付ける
		fireCallback: function (event, args) {
			var method = 'on' + event;
			if (!this.instance) {
				this.instance = document[this.id];
				this.instance.initialize(this.options);
				this.instance.setEnabled();
			}
			if (this[method]) {
				this[method](args);
			}
		}
		
	}
};

/**
 * FancyUpload-jQuery実体。
 * さすがにオリジナルそのままの実装は難しそうだ…。
 */
var FancyUpload = function (status, list, arg) {
	var upload = new Uploader ();
	
	// 引数バリデート
	var validate = new FancyUploadArgs ();
	jQuery.each (arg, function(i, val) {
		validate[i] = val;
	});

	if (validate.id) {
		var id = validate.id;
	} else {
		var id = 'swf_' + (Date.now() || +new Date);
	}
	upload.id = id;

	// 各オプションの設定
	upload.target = validate.target;
	upload.action = validate.action;
	upload.data = validate.data;
	upload.mergeData = validate.mergeData;
	upload.fieldName = validate.fieldName;

	jQuery.each (upload.options, function(i, val) {
		if (typeof validate[i] != 'undefined') {
			upload.options[i] = validate[i];
		}
	});
	upload.options.url = Swiff.qualifyPath(validate.url);

	// コールバックの登録
	jQuery.each (validate.Callbacks, function(i, val) {
		upload[i] = val;
	});
	
	jQuery.each(validate.properties, function(i, val){
		upload.properties[i] = val;
	});
	var properties = upload.properties;
	
	properties.height = validate.height;
	properties.width = validate.width;
	properties.zIndex = validate.zIndex;

	if (jQuery.browser.msie){
		upload.properties.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
		upload.params.movie = validate.path;
	} else {
		upload.properties.type = 'application/x-shockwave-flash';
		upload.properties.data = validate.path;
	}

	var build = '<object id="' + id + '"';

	for (var property in upload.properties) build += ' ' + property + '="' + upload.properties[property] + '"';
	build += '>';

	jQuery.each(validate.params, function(i, val){
		upload.params[i] = val;
	});
	var params = upload.params;

	params.flashVars = "fireCallback=Swiff.Uploader." + id + ".fireCallback";

	for (var param in params){
		if (upload.params[param]) build += '<param name="' + param + '" value="' + upload.params[param] + '" />';
	}

	build += '</object>';

	upload.status = status;
	upload.list = list;
	upload.fallback = validate.fallback;
	
	var target = $(validate.target);
	if (target) {
		$(status).append (build);

		$(upload.fallback).remove();
		
		upload.setPosition();
		Swiff.register (upload);
	}

	return;
};

/**
 * FancyUploadに渡すオプションのバリデート用
 */
var FancyUploadArgs = function () { return {
		id: null,

		path: "./source/Swiff.Uploader.swf",
		height: 30,
		width: 100,
		container: null,
		properties: {},
		params: {
			wMode: "transparent",
			menu: "false",
			allowScriptAccess: "always",
			quality: "high",
			swLiveConnect: "true",
		},
		Callbacks: {},
		vars: {},

		target: null,
		fallback: null,
		zIndex: 9999,
		height: 30,
		width: 100,

		// swfに渡すオプション
		typeFilter: null,
		typeFilterDescription: null,
		multiple: null,
		queued: null,
		verbose: null,
		width: 128,
		height: 32,
		passStatus: null,
		
		url: null,
		method: null,
		data: null,
		mergeData: null,
		fieldName: null,
		progressGraphSize: 10,
		
		fileSizeMin: 1,
		fileSizeMax: null, // Official limit is 100 MB for FileReference, but I tested up to 2Gb!
		allowDuplicates: false,
		timeLimit: 30,
//		timeLimit: (Browser.Platform.linux) ? 0 : 30,
		
		policyFile: null,
		buttonImage: null,
		// ここまで
			
		fileListMax: 0,
		fileListSizeMax: 0,

		instantStart: false,
		appendCookieData: false,
		
		fileClass: null
		/*
		onLoad: $empty,
		onFail: $empty,
		onStart: $empty,
		onQueue: $empty,
		onComplete: $empty,
		onBrowse: $empty,
		onDisabledBrowse: $empty,
		onCancel: $empty,
		onSelect: $empty,
		onSelectSuccess: $empty,
		onSelectFail: $empty,
		
		onButtonEnter: $empty,
		onButtonLeave: $empty,
		onButtonDown: $empty,
		onButtonDisable: $empty,
		
		onFileStart: $empty,
		onFileStop: $empty,
		onFileRequeue: $empty,
		onFileOpen: $empty,
		onFileProgress: $empty,
		onFileComplete: $empty,
		onFileRemove: $empty,
		
		onBeforeStart: $empty,
		onBeforeStop: $empty,
		onBeforeRemove: $empty
		*/
};};

